import React, { useReducer } from 'react';
import ReactDOM from 'react-dom';
// 纯函数：函数的执行不会对外界有任何副作用；
function reducer(state, action) {
  // 这个函数是用来修改数据并且触发视图更新的
  console.log(action)
  switch (action.type) {
    case 'changeName':
      state.name = action.nnn
      break;
    case 'changeAge':
      state.age = action.aaa
      break;
    case 'changeSex':
      state.sex = action.sss
      break;
  }
  return {
    ...state
  }
}
function App() {
  // name age sex
  let [state, dispatch] = useReducer(reducer, {
    name: '珠峰',
    age: 13,
    sex: 0
  })
  return <>
    <h1>{JSON.stringify(state)}</h1>
    <button onClick={() => dispatch({ type: "changeName", nnn: 111 })}>name</button>
    <button onClick={() => dispatch({ type: "changeAge", aaa: 222 })}>age</button>
    <button onClick={() => dispatch({ type: "changeSex", sss: 333 })}>sex</button>
  </>
}

ReactDOM.render(<App />, document.getElementById('root'))